package com.gitee.wsl.common.chart.sample.line

import android.graphics.Canvas
import com.gitee.wsl.android.ui.ext.type.drawLine
import com.gitee.wsl.common.chart.ChartEnum
import com.gitee.wsl.common.chart.adapter.DataSource
import com.gitee.wsl.common.chart.listener.click.PlotPointPosition
import com.gitee.wsl.common.chart.render.background.axis.line.CategoryLineAxis
import com.gitee.wsl.common.chart.render.background.axis.line.DataLineAxis
import com.gitee.wsl.common.chart.render.shape.plot.renderDot
import com.gitee.wsl.common.chart.sample.line.adapter.LineChartAdapter
import com.gitee.wsl.common.chart.sample.line.model.ValueLineData
import com.gitee.wsl.common.ui.draw.type.DotStyle
import com.gitee.wsl.math.Math
import com.gitee.wsl.jvm.math.add
import com.gitee.wsl.jvm.math.div
import com.gitee.wsl.jvm.math.mul
import com.gitee.wsl.jvm.math.sub

import timber.log.Timber


/**
 * @ClassName LineChart
 * @Description  线图基类
 * @author XiongChuanLiang<br/>(xcl_168@aliyun.com)
 *
 */

class ValueLineChart(val adapter: LineChartAdapter<ValueLineData> = LineChartAdapter()):AbstractLineChart(adapter) {
    //数据源
    var dataSet by DataSource(adapter)

    //当线与轴交叉时是否不断开连接
     var isLineAxisIntersectVisible = true

    override fun getType(): ChartEnum.ChartType{
        return ChartEnum.ChartType.LINE
    }

    /**
     * 绘制线
     * @param canvas    画布
     * @param bd        数据类
     * @param type        处理类型
     */
    private fun drawLine(canvas: Canvas, bd: ValueLineData, type: String, dataID: Int, dataAxis:DataLineAxis, categoryAxis: CategoryLineAxis): Boolean {

        val initX: Float = bodyViewport.left
        val initY: Float = bodyViewport.bottom
        var lineStartX = initX
        var lineStartY = initY
        var lineStopX = 0.0f
        var lineStopY = 0.0f

        //得到分类轴数据集
        val dataSet: List<String> = categoryAxis.dataSet
        if (dataSet.isEmpty()) {
            Timber.d("分类轴数据为空.")
            return false
        }
        //数据序列
        val chartValues = bd.linePointList
        if ( chartValues.size == 0) {
            Timber.d("当前分类的线数据序列值为空.")
            return false
        }

        //步长	
        var XSteps = 0.0f
        var j = 0 //,childID = 0;	
        val tickCount = dataSet.size
        if (1 == tickCount) j = 1 //label仅一个时右移 !mXCoordFirstTickmarksBegin && 
        val labeltickCount = getCategoryAxisLabelTickCount(categoryAxis)
        XSteps = getVerticalXSteps(labeltickCount)
        val itemAngle = bd.itemLabelRotateAngle
        val pLine = bd.plotLine
        val pDot = pLine.plotDot
        val radius = pDot.dotRadius
        var bv = 0.0f

        //画线
        val count = chartValues.size
        for (i in 0 until count) {
            bv = chartValues[i]
            lineStopY = getVPValPosition(bv,dataAxis)
            if (xCoordFirstTickmarksBegin) {
                lineStopX = Math.add(initX, Math.mul((j + 1).toFloat(), XSteps))
            } else {
                lineStopX = Math.add(initX, Math.mul(j.toFloat(), XSteps))
            }
            //当柱图与线图混合，且柱图柱形为BarCenterStyle.SPACE时
            if (xCoordFirstTickmarksBegin && ChartEnum.BarCenterStyle.SPACE == barCenterStyle)
                lineStopX = Math.sub(lineStopX, Math.div(XSteps, 2f))
            if (0 == j) {
                lineStartX = lineStopX
                lineStartY = lineStopY
            }
            if (!isLineAxisIntersectVisible && bv.compareTo(dataAxis.axisMin) == 0) {
                //如果值与最小值相等，即到了轴上，则忽略掉  
                lineStartX = lineStopX
                lineStartY = lineStopY
                j++
            } else {
                if (type.equals("LINE", ignoreCase = true)) {
                    if (isLineAxisIntersectVisible || lineStartY.compareTo(initY) != 0) {
                        canvas.drawLine(
                            bd.lineSpacingStyle,
                            lineStartX, lineStartY, lineStopX, lineStopY,
                             pLine.linePaint
                        )
                    }
                } else if (type.equals("DOT2LABEL", ignoreCase = true)) {
                    if (pLine.dotStyle != DotStyle.HIDE) {
                        canvas.renderDot(pDot, lineStopX, lineStopY, pLine.dotPaint) //标识图形
                        savePointRecord(
                            dataID, i, lineStopX + moveX, lineStopY + moveY,
                            lineStopX - radius + moveX,
                            lineStopY - radius + moveY,
                            lineStopX + radius + moveX,
                            lineStopY + radius + moveY
                        )
                        lineStopX = Math.add(lineStopX, radius)
                    }

                    //显示批注形状
                    drawAnchor(anchorSet, dataID, i, canvas, lineStopX - radius, lineStopY, radius)
                    if (bd.isLabelVisible) //标签
                    {
                        bd.plotLabel.renderLabel(
                            canvas, pLine.dotLabelPaint,
                            getFormatterItemLabel(bv), lineStopX - radius, lineStopY,
                            itemAngle, bd.lineColor
                        )
                    }
                } else {
                   Timber.d("未知的参数标识。") //我不认识你，我不认识你。
                    return false
                }
                lineStartX = lineStopX
                lineStartY = lineStopY
                j++
            } //if(bv != dataAxis.getAxisMin())
        }
        adapter.isRecordSetInited=true
        return true
    }

    /**
     * 绘制图表
     */
    private fun drawVerticalPlot(canvas: Canvas, dataAxis:DataLineAxis, categoryAxis: CategoryLineAxis): Boolean {
        lstKey.clear()
        var key = ""
        //开始处 X 轴 即分类轴                  
        val count = dataSet.size
        for (i in 0 until count) {
            if (!drawLine(canvas, dataSet[i], "LINE", i,dataAxis, categoryAxis)) return false
            if (!drawLine(canvas, dataSet[i], "DOT2LABEL", i,dataAxis, categoryAxis)) return false
            key = dataSet[i].label
            if ("" !== key && key.isNotEmpty()) lstKey.add(dataSet[i])
        }
        return true
    }

    /////////////////////////////////////////////

    /////////////////////////////////////////////
    override fun drawClipPlot(canvas: Canvas, dataAxis:DataLineAxis, categoryAxis:CategoryLineAxis) {
        if (drawVerticalPlot(canvas,dataAxis, categoryAxis)) {
            if (null != customLine) //画横向定制线
            {
                customLine!!.setVerticalPlot(dataAxis, plotViewport, getAxisScreenHeight())
                customLine!!.renderVerticalCustomlinesDataAxis(canvas)
            }
        }
    }

     fun savePointRecord(
        dataID: Int, childID: Int,
        x: Float, y: Float, left: Float, top: Float, right: Float, bottom: Float
    ) {
        if(adapter.isRecordSetInited) return
        val pRecord = PlotPointPosition()
        pRecord.savePlotDataID(dataID)
        pRecord.savePlotDataChildID(childID)
        pRecord.savePlotPosition(x, y)
        pRecord.savePlotRectF(left, top, right, bottom)
        //pRecord.extPointClickRange(clickRangeExtValue)
        adapter.addRecordItem(pRecord)
    }

     override fun drawClipLegend(canvas: Canvas) {
        //plotLegend.renderLineKey(canvas, mLstKey)
        //mLstKey.clear()
    }
    /////////////////////////////////////////////
}